package gui.scheduler;

import java.awt.*;
import java.awt.color.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import java.util.*;

import gui.SearchGUI;
import util.UserData;

import meal.Meal;

import util.CalendarInterface;

public class Cell extends JPanel implements MouseListener, ActionListener {
	//static public enum State { DEFAULT, DELETED, FILLED };
	// Old should be independent of filled state
	final public static int DEFAULT = 0;
	final public static int DELETED = 1;
	final public static int FILLED = 2;
	final public static int USER = 3;

	// white, red, green
	private final static Color BGColor[]= { new Color(255,255,255), new Color(255,227,227), new Color(227,255,250),
		new Color(255,223,138), new Color(250,250,250), new Color(255,240,240), new Color(240,255,250),
		new Color(255,237,189)};
	private final static Color FGColor[]= { Color.darkGray, Color.red.darker(), new Color(0,100,10),
		new Color(66,48,0),new Color(215,215,215), new Color(255,230,230), new Color(200,235,230),
		new Color(172,143,64)};

	private JLabel text;
	private JLabel day;

	private boolean old;
	private boolean usr;
	private Calendar date;
	private int dayOfWeek;
	private PopupMenu menu;
	private Border border;
	private static String[] search_terms=null; {
		if(search_terms==null) {
			Meal[] tmp = util.IO.importMeals("recipes/");
			ArrayList<String> s = new ArrayList<String>();
			for(Meal meal : tmp)
				if(meal!=null)
					s.add(meal.getName());
			search_terms=s.toArray(new String[] { null });
		}
	}

	private MenuItem menuItems[] = { new MenuItem("Choose Meal"),
		new MenuItem("Delete"), new MenuItem("Skip")};

	public Cell() {
		SpringLayout layout = new SpringLayout();
		this.setLayout(layout);

		this.menu = new PopupMenu();
		this.add(this.menu);
		menu.addActionListener(this);

		for(MenuItem item : menuItems)
			menu.add(item);

		menuItems[1].setEnabled(false);

		this.day = new JLabel("", JLabel.LEFT);
		this.add(this.day);

		this.text = new JLabel("", JLabel.CENTER);
		this.add(this.text);

		this.usr=false;

		layout.putConstraint(SpringLayout.WEST, this.day, 0, SpringLayout.WEST, this );
		layout.putConstraint(SpringLayout.NORTH, this.day, 0, SpringLayout.NORTH, this );
		layout.putConstraint(SpringLayout.WEST, this.text, 3, SpringLayout.WEST, this );
		layout.putConstraint(SpringLayout.EAST, this.text, -3, SpringLayout.EAST, this );
		layout.putConstraint(SpringLayout.VERTICAL_CENTER, this.text, 0, SpringLayout.VERTICAL_CENTER, this );

		this.setPreferredSize(new Dimension(90,70));
		this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

		this.addMouseListener(this);
	}
	public void setFields(Calendar date, boolean old, String meal) {
		this.day.setText(String.valueOf(date.get(Calendar.DATE)));
		this.old=old;
		this.date = (Calendar) date.clone();
		dayOfWeek=date.get(Calendar.DAY_OF_WEEK);
		setMeal(meal);
	}
	public void setMeal(String meal) {
		this.text.setText(meal);
		this.setToolTipText(meal);
		int state;

		if(meal==null) {
			if(!UserData.shouldGenerate(dayOfWeek)
					|| date.before(UserData.getGeneratedTo()))
				state=DELETED;
			else
				state=DEFAULT;
			menuItems[1].setEnabled(false);
		} else {
			if(this.usr)//UserData.shouldGenerate(dayOfWeek))
				state=USER;
			else
				state=FILLED;
			menuItems[1].setEnabled(true);
		}

		if(old) state+=4;

		this.day.setForeground(FGColor[state]);
		this.text.setForeground(FGColor[state]);
		this.setBackground(BGColor[state]);
	}
	public void setUserMade(boolean b) {
		this.usr=b;
	}
	public void setBorder(Border border) {
		super.setBorder(border);
		this.border=border;
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		if(e.getButton()==MouseEvent.BUTTON3)
			menu.show(this, e.getX(), e.getY());
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		super.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED,
				new Color(200,200,255),new Color(230,230,255)));
	}
	@Override
	public void mouseExited(MouseEvent e) {
		super.setBorder(border);
	}
	@Override
	public void mousePressed(MouseEvent e) {
		super.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED,
				new Color(150,150,255),new Color(150,150,255)));
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		if(e.getButton()==MouseEvent.BUTTON3)
			menu.show(this, e.getX(), e.getY());
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		switch(e.getActionCommand().charAt(0)) {
			case 'C': // Choose Meal
				super.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED,
					new Color(200,200,255),new Color(230,230,255)));
				String str= SearchGUI.getMeal(search_terms,
					this);
				if(str!=null)
					if(str.length()==0) {
						setMeal(null);
					} else {
						setUserMade(true);
						setMeal(str);
						CalendarInterface.MealEvent evnt = new CalendarInterface.MealEvent();
						evnt.setDate(date.getTime());
						evnt.setMealName(str);
						evnt.setDescription("I'll handle this later...");
						evnt.setSource("USR");
						CalendarInterface.addMealEvent(evnt);
					}
				break;
			case 'D': // Delete
				setMeal(null);
				try {
					CalendarInterface.deleteMealsByDate(date.getTime());
				} catch(Exception exc) {
					exc.printStackTrace();
				}
				break;
			case 'S': // Skip
				break;
			case 'U': // Unskip
				break;
		}
	}
}
